﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.DAO;
using PPAP.Model;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.BasicManage
{
    public partial class PasswordReset : PageBase
    {
        protected System.Web.UI.WebControls.TextBox txtUserName;
        LogBO lbo = new LogBO();

        protected void Page_Load(object sender, System.EventArgs e)
        {
            //this.BtnSave.Attributes.Add("onclick","return CheckForm();");
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void BtnSave_Click(object sender, System.EventArgs e)
        {

            //UserBiz ubiz=new UserBiz(); 
            UserDAO udao = new UserDAO();
            string oldPassword = this.txtOldPassword.Text.Trim();

            string Newpassword = this.txtNewPassword.Text.Trim();
            string Truepassword = this.txtTruePassword.Text.Trim();

            if (oldPassword == "")
            {
                base.AlertMessage("The User Name can't for empty");
                setFocus("txtOldPassword");
                return;
            }
            if (Newpassword == "")
            {
                base.AlertMessage("The new password can't for empty");
                setFocus("txtNewPassword");
                return;
            }
            if (Newpassword != Truepassword)
            {
                base.AlertMessage("Confirm password mistake！");
                setFocus("txtTruePassword");
                return;
            }
            if (Newpassword.Length < 6)
            {
                base.AlertMessage("Password too short(At least six)！");
                setFocus("txtTruePassword");
                return;
            }
            //bool exist;//= ubiz.CheckUser(oldPassword);
            //exist = udao.CheckUser(CurrentUser.Id, oldPassword);
            //if (exist)
            //{
                int num = 0;
                DataSet ds = new UserDAO().GetUserByUserName(oldPassword);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    switch (ds.Tables[0].Rows[0]["ctype"].ToString())
                    {
                        case "0":
                            ContractPersonDao dao = new ContractPersonDao();
                            ContractPerson sup = dao.GetModelByEmail(oldPassword);
                            if (sup != null)
                            {
                                sup.Password = Newpassword;
                                num = dao.Update(sup);
                            }
                            break;
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                        case "5":
                        case "6":
                        case "7":
                        case "8":
                        case "9":
                            UsersDAO qdao = new UsersDAO();
                            Users qm = qdao.GetModelByUserName(oldPassword);
                            if (qm != null)
                            {
                                qm.PASSWORD = Newpassword;
                                qm.TESTDEPARTMENT = qm.TESTDEPARTMENT == null ? 0 : qm.TESTDEPARTMENT;
                                qm.Superior1 = qm.Superior1 == null ? "" : qm.Superior1;
                                qm.Superior2 = qm.Superior2 == null ? "" : qm.Superior2;
                                num = qdao.Update(qm);
                            }
                            break;
                        case "99":
                            SupplierAdminDAO sadao = new SupplierAdminDAO();
                            SupplierAdmin sa = sadao.GetModel(int.Parse(ds.Tables[0].Rows[0]["SupplierCode"].ToString()));
                            sa.PASSWORD = Newpassword;
                            num = sadao.Update(sa);

                            break;
                        default:
                            break;
                    }
                    if (num >0)
                    {
                        lbo.WriteLog(CurrentUser.Id, "PasswordReset");
                        base.AlertReLoad("Password updated！");
                    }
                    else
                    {
                        base.AlertReLoad("Password updated failure！");
                    }
                }
                else
                {
                    AlertMessage("The user name does not exist！");
                }
            //}
            //else
            //{
            //    base.AlertMessage("The original password is not correct！");
            //    setFocus("txtOldPassword");

            //}
        }
        private void setFocus(string TextID)
        {
            string Startup = "<script language=javascript> ";
            Startup += "document.Form1." + TextID + ".focus();";
            Startup += " document.Form1." + TextID + ".style.backgroundColor = \"#cccccc\";";
            Startup += "</script>";
            RegisterStartupScript("Startup", Startup);
        }

        protected void btnSave1_Click(object sender, EventArgs e)
        {
            BtnSave_Click(null, null);
        }
    }
}
